package com.immediatus.graphics.vertex;

import com.immediatus.util.FastFloatBuffer;

public class RectangleVertexBuffer extends VertexBuffer{

    public static final int VERTICES_PER_RECTANGLE = 4;
    private static final int FLOAT_TO_RAW_INT_BITS_ZERO = Float.floatToRawIntBits(0);

    public RectangleVertexBuffer(final int drawType_, final boolean managed_){
        super(2 * VERTICES_PER_RECTANGLE, drawType_, managed_);
    }

    public synchronized void update(final float width_, final float height_){
        final int x = FLOAT_TO_RAW_INT_BITS_ZERO;
        final int y = FLOAT_TO_RAW_INT_BITS_ZERO;
        final int x2 = Float.floatToRawIntBits(width_);
        final int y2 = Float.floatToRawIntBits(height_);

        final int[] bufferData = this._BufferData;
        bufferData[0] = x;
        bufferData[1] = y;

        bufferData[2] = x;
        bufferData[3] = y2;

        bufferData[4] = x2;
        bufferData[5] = y;

        bufferData[6] = x2;
        bufferData[7] = y2;

        final FastFloatBuffer buffer = this.getFloatBuffer();
        buffer.position(0);
        buffer.put(bufferData);
        buffer.position(0);

        super.setHardwareBufferNeedsUpdate();
    }
}
